<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/21
 * Time: 21:44
 */
namespace app\admin\model\fanyi\bind;
use app\admin\model\curl\Curl;
use think\Exception;

class Baidu implements Config{
    protected $q = '';//要翻译的字符
    protected $from = 'auto';//字符串语言
    protected $to = 'en';//翻译成的语言
    public  $error = '';//报错
    public function __construct(array $config = [])
    {
        $array = config('fanyi.baidu');
        foreach ($array as $key=>$value){
            $this->$key = $value;
        }
        if($config){
            foreach ($config as $key=>$value){
                $this->$key = $value;
            }
        }
        return $this;
    }

    /**设置翻译的字符
     * @param null $str
     * @return $this
     */
    public function setQ($str=null){
        if($str){
            $this->q = mb_convert_encoding($str,"UTF-8");
        }
        return $this;
    }

    /**获取翻译的字符
     * @return string
     */
    public function getQ(){
        if($this->q){
            return $this->q;
        }else{
            throw new Exception('请设置要翻译的字符串');
        }
    }
    /**设置发送语言
     * @param null $form
     * @return $this
     */
    public function setForm($form=null){
        if($form){
            $this->from = $form;
        }
        return $this;
    }

    /**获取发送语言
     * @return string
     * @throws Exception
     */
    public function getForm(){
        if($this->from){
            return $this->from;
        }else{
            throw new Exception('请设置发送语言');
        }
    }

    /**设置翻译成的语言
     * @param null $to
     * @return $this
     */
    public function setTo($to=null){
        if($to){
            $this->to = $to;
        }
        return $this;
    }

    /**获取翻译成的语言
     * @return string
     * @throws Exception
     */
    public function getTo(){
        if($this->to){
            return $this->to;
        }else{
            throw new Exception('请设置翻译成的语言');
        }
    }

    /**翻译
     * @param string要翻译成的语言 $to
     * @param null翻译的字符串 $str
     * @param bool如果要src传src $type
     * @return bool
     * @throws Exception
     */
    public function getFanyi($to='en',$str=null,$type=true){
        if($str){
            $this->setQ($str);
        }
        if($to){
            $this->setTo($to);
        }
        $data['q'] = $this->q;
        $data['from'] = $this->from;
        $data['to'] = $this->to;
        $data['appid'] = $this->appid;
        $data['salt'] = uniqid();
        $data['sign'] = $this->getsign($data);
        $url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
        $obj = new Curl();
        $array = $obj->fasong($url,1,1,$data)->jsonToArray()->get();
//        return $array;
        if($array['trans_result'][0]['dst']??null){
            if($type){
                return $array['trans_result'][0]['dst'];
            }
            if($type=='src'){
                return $array['trans_result'][0]['src'];
            }
            return $array['trans_result'][0];
        }else{
            $this->error = $array;
            return false;
        }
    }

    /**获取签名
     * @param array $data
     * @return string
     * @throws Exception
     */
    public function getsign($data=[]){
        $str = '';
        if($data['appid']??null){
            $str .=$data['appid'];
        }else{
            throw new Exception('请设置appid');
        }
        if($data['q']??null){
            $str .=$data['q'];
        }else{
            throw new Exception('请设置要翻译的字符');
        }
        if($data['salt']??null){
            $str .=$data['salt'];
        }else{
            throw new Exception('请设置随机数');
        }
        $str .= $this->key;
        return md5($str);
    }
}